<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\SmartTagRequest;
use App\CustHub\Service\ChSmartTagContactStatService;
use App\CustHub\Service\ChSmartTagService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 智能标签
 */
#[Controller(prefix: 'ch/smartTag'),ApiAuth,VerifySignature]
class SmartTagController extends BaseController
{
    #[Inject]
    protected ChSmartTagService $service;

    public array $noNeedLogin = ['exportContactStats','exportDailyStats'];
    public array $noNeedVerifySign = ['exportContactStats','exportDailyStats'];

    /**
     * 标签列表
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getTagList'),ChPermission('ch:smart:tag:list')]
    public function getTagList(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->service->getListByUidAndGroupId($request->all());
        return $this->success($data);
    }

    /**
     * 新增标签组
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save'),ChPermission('ch:smart:tag:save'),ChOperationLog]
    public function save(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(['id'=>$this->service->save($request->all())]);
    }

    /**
     * 更新标签
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('update'),ChPermission('ch:smart:tag:update'),ChOperationLog]
    public function update(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->update($request->input('id'),$request->all()) ? $this->success() : $this->error();
    }

    /**
     * 批量新增标签
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchSave'),ChPermission('ch:smart:tag:batch:save'),ChOperationLog]
    public function batchSaveTag(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchSaveTag($request->all())  ? $this->success() : $this->error();
    }

    /**
     * 删除标签组
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete'),ChPermission('ch:smart:tag:delete'),ChOperationLog]
    public function deleteTagGroup(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->deleteTagGroup($request->all())  ? $this->success() : $this->error();
    }

    /**
     * 批量删除标签
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchDeleteTag'),ChPermission('ch:smart:tag:delete:tag'),ChOperationLog]
    public function batchDeleteTag(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchDeleteTag($request->all())  ? $this->success() : $this->error();
    }

    /**
     * 标签排序
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('sort')]
    public function sort(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $params = $request->all();
        $this->service->sort($params);
        return $this->success();
    }

    /**
     * 智能标签选择列表
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('selectList')]
    public function selectList(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->selectList($this->request->all()));
    }

    /**
     * 添加客户
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('addContact'),ChPermission('ch:smart:tag:add:contact'),ChOperationLog]
    public function addContact(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->addContact($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 标签里删除客户
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delContact'),ChPermission('ch:smart:tag:del:contact'),ChOperationLog]
    public function delContact(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->delContact($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 移动标签
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('moveContact'),ChPermission('ch:smart:tag:move:contact'),ChOperationLog]
    public function moveContact(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->moveContact($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 获取客户统计
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getContactStats')]
    public function getContactStats(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $result = container()->get(ChSmartTagContactStatService::class)->getStatsList($request->all());
        return $this->success($result);
    }

    /**
     * 导出客户统计
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[GetMapping('exportContactStats')]
    public function exportContactStats(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return container()->get(ChSmartTagContactStatService::class)->exportContactStats($request->all());
    }


    /**
     * 获取标签占比数据
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getTagProportionData')]
    public function getTagProportionData(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getTagProportionData($request->all()));
    }


    /**
     * 获取对比昨日数据
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getYesterdayComparisonData')]
    public function getYesterdayComparisonData(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getYesterdayComparisonData($request->all()));
    }

    /**
     * 获取按天统计数据
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getDailyStats')]
    public function getDailyStats(SmartTagRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getDailyStats($request->all()));
    }

    /**
     * 导出数据
     * @param SmartTagRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    #[GetMapping('exportDailyStats')]
    public function exportDailyStats(SmartTagRequest $request):\Psr\Http\Message\ResponseInterface
    {
        return $this->service->exportDailyStats($this->request->all());
    }
}